import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    ManyToOne,
    Unique,
} from 'typeorm';

import { Role } from '@/modules/system/role/entities/role.entity';
import { User } from '@/modules/system/user/entities/user.entity';
import { TABLE_PREFIX } from '@/config';

@Entity({ name: `${TABLE_PREFIX}sys_user_role` }) //设置表名为'user_groups'，与TypeORM自动生成的表名相同
@Unique(['user', 'role']) // 添加唯一约束，避免重复的user-role关系
export class UserRole {
    @PrimaryGeneratedColumn()
    id: number;
    
    @Column('int')
    userId: number;
    
    @Column('int')
    roleId: number;
    
    @ManyToOne(() => User, (user) => user.id, { onDelete: 'CASCADE' })
        // @JoinColumn({ name: 'userId' }) // 设置列名为'userId'，与TypeORM自动生成的列名相同
    user: User;
    
    @ManyToOne(() => Role, (role) => role.id, { onDelete: 'CASCADE' })
        // @JoinColumn({ name: 'roleId' }) // 设置列名为'roleId'，与TypeORM自动生成的列名相同
    role: Role;
    
    // 添加其他中间表的字段，例如：角色、权限等
}